window.onload = function (){
    findAll();
}

/**
 * 查询所有频道
 */
function findAll(){
    axios.get("/channel/findAll").then(resp =>{
        var info = resp.data;
        var str = "";
        info.forEach(n =>{
            str += `<tr><td>${n.name}</td><td>${n.channelDescribe}</td>
                    <td>${n.postNum}</td><td><input type="button" value="查看详情" onclick="lookPost(${n.id})"></td></tr>`;

        })
        $("data").innerHTML = str;
    });
    axios.get("user/getLoginUser").then(resp => {
        var info = resp.data;
        if (info == '' || info == null) {
            $("loginDiv").innerHTML = "请先<a href='/login.html'>登录</a>"
        } else {
            $("loginDiv").innerHTML = "欢迎" + info.userName;
            $("faceImg").src="/face/"+info.imgUrl;
            $("userNameSpan").innerHTML=info.userName;
            $("mailSpan").innerHTML=info.mail;
        }
    })
}

function exitIndex(){
    $("hidDiv").style.visibility="hidden";
}
/**
 * 查看主贴
 */
function lookPost(channelId){
    $("hidDiv").style.visibility="visible";
    findByItem(1,channelId);

}

/**
 * 动态条件分页查询
 * @param pageNO 页码
 */
function findByItem(pageNO,channelId) {
    axios.get("/post/findByItem", {
        params: {
            pageNO,
            channelId:channelId,
            tittle: $("tittleTxt").value,
            startDate: $("startTxt").value,
            endDate: $("endTxt").value
        }
    }).then(resp => {
        var info = resp.data;
        //表格
        var str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.theme}</td><td>${n.msg}</td><td>${n.commentNum}</td><td>${n.localDate}</td><td><input type="button" value="查看回帖"></td></tr>`;
        });
        $("dataPost").innerHTML = str;

        //页码
        var pageStr = "";
        for (var i = 1; i <= info.totalPage; i++) {
            pageStr += `<a href="javascript:findByItem(${i})" style="margin-left: 10px">${i}</a>`;
        }
        $("cutDiv").innerHTML = pageStr;
    })
}

